package com.test.sdk.common.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.*;

/**
 * @author 徒有琴
 */
public class SignUtil {
    // 把请求中的参数按照key的升序排列 sign= MD5(key1=v1&key2=v2+秘钥)
    public static String getSignStr(Map<String, String[]> params) {
        Set<String> set = params.keySet();
        List<String> keys = new ArrayList<>(set);
        Collections.sort(keys);//默认升序
        StringBuilder builder = new StringBuilder();
        for (String key : keys) {
            if ("sign".equals(key)) {
                continue;
            }
            builder.append(key).append("=");
            String[] values = params.get(key);
            if (values != null && values.length > 0) {
                    System.out.println(values[0]);
                    builder.append(values[0]);
            }
            builder.append("&");
        }
        return builder.toString();
    }
}
